package com.thd.springboot.framework.cloud.eureka.controller;

import com.netflix.discovery.shared.Application;
import com.netflix.eureka.EurekaServerContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * com.thd.springboot.framework.example.controller.RegistCenter
 *
 * @author: wanglei62
 * @DATE: 2022/9/9 11:11
 **/
@Controller
@RequestMapping("/registCenter")
public class RegistCenterController {
    private Logger logger = LoggerFactory.getLogger(RegistCenterController.class);
    /**
     * 获取注册的服务列表信息
     * @return
     */
    @RequestMapping(value="/serviceList",produces = { "application/json;charset=UTF-8" })
    @ResponseBody
    public ResponseEntity<List<Application>> serviceList(){
        logger.info("list all service information in eureka");
        List<Application> applications = EurekaServerContextHolder.getInstance().getServerContext().getRegistry().getSortedApplications();
// 判断SMC是否启动，使用标记控制唯一节点启动
        for (Application application : applications) {
            String name = application.getName();
        }
        return ResponseEntity.ok(applications);
    }
}
